/**
 * @author 刘超
 * @Note 数字关系规则.
 * @UsePage: [Health Rule]
 * @version 1.0
 */
var Unionmon_DRR = function (params){
    this.expression = "";						支持参数赋值
    this.DA = []; // 数据数组
    this.DO = new Object(); // 数据对象
    this.Doms = new Object(); // 元素对象
    this.domsSize = 0;
    this.domStartPosition = 0; // 代表对象所在数组的位置					支持参数赋值
    this.domIndexStr = "Index"; // 代表对象所在数组的位置					支持参数赋值
    this.domIsLastStr = "IsLast"; // 代表对象是否是最后一位				支持参数赋值
    this.domAtIndex = "atIndex"; // 元素所在的第几个位置					支持参数赋值
    this.isAutoInit = false; // 该对象是否是自动初始化的					支持参数赋值
    this.findTimes = 100; // 寻找DOM元素的次数							支持参数赋值
    this.currTimes = 0; // 当前寻找DOM元素的次数							支持参数赋值
    this.findInterval = 10; // 寻找DOM元素的时间间隔						支持参数赋值
    this.divStatusMonitoring = 2000; // 监控Div状态时间间隔				支持参数赋值
    this.isFindDoms = false; // 元素是否全部找到了
    this.sto = null; // setTimeout 对象
    this.div = null;
    this.divId = "drr_Unionmon_Div";//						支持参数赋值
    this.spanName = "drrSpan";//							支持参数赋值
    this.spanIdSuf = "[spanIdSuf]";//						支持参数赋值
    this.spanWidth = 26;//									支持参数赋值
    this.spanBorder = "2px solid gray";//					支持参数赋值
    this.spanColor = null;//								支持参数赋值
    this.spanValue = "[spanValue]";
    this.spanStr = this.defaultSpanStr(); // 默认span样式
    this.isClosedDiv = {s1:true,s2:true}; // s switch 开关的意思
    this.isClosedDivBackup = {s1:true,s2:true}; // s switch 开关的意思
    this.digitalInterval = 1; // 数字间隔					支持参数赋值
    this.contentDisplay = "itself"; // Div 内容显示方式 [all|itself][全部|本身]		支持参数赋值
    this.contentDisplayOption = "[itself|all]";//			支持参数赋值


    if(arguments.length < 1){ // 不会自动初始化操作
    	return ;
    }
    this.isAutoInit = true;
    this.initParams(params); // 初始化属性
    this.createDiv(); // 自动生成一个div
    this.decompose(); // 分解字符串中包含的元素
    this.autoFindDoms(); // 寻找元素;
    this.bindEvents(); // 绑定事件
    this.automaticControlDiv(); // 自动监控Div状态以确定是否自动关闭
};
Unionmon_DRR.prototype.defaultSpanStr = function (){
	var str = "";

	str += "<span ";
	str += "id="+this.spanName+this.spanIdSuf+" ";
	str += "style='"; // 样式开始
	str += "width:" + this.spanWidth +";";
	str += "border:" + this.spanBorder + ";";
	str += "cursor:hand;";
	str += "text-align:center;";
	str += "margin:2px;";
	str += "' ";       // 样式结束
	str += ">";
	str += this.spanValue;
	str += "</span>";

    return str;
};
Unionmon_DRR.prototype.$ = function (id){
	return document.getElementById(id);
};
Unionmon_DRR.prototype.initParams = function (params){
	if(!params){
		return false;
	}
	for(var key in params){
		this[key] = params[key];
	}
};
Unionmon_DRR.prototype.createDiv = function (){
	var this_ = this;
	function start(){
		if(document.body){
			var div = document.createElement("div");
			div.id = this_.divId;
			div.style.width = "120";
			div.style.height = "100";
			div.style.position = "absolute";
			div.style.border = "1px solid blue";
			div.style.background = "white";
			div.style.zIndex = 100;
			div.style.display = "none";
			div.style.overflow = "auto";
			document.body.appendChild(div);

			div.onmouseout = function (){ // 设置是否关闭Div
				this_.isClosedDiv.s2 = true;
			};
			div.onmouseover = function (){
				this_.isClosedDiv.s2 = false;
			};

			this_.div = div;
		}else{
			setTimeout(start, this_.findInterval);
		}
	}
	start();
};
Unionmon_DRR.prototype.automaticControlDiv = function (id){
	var this_ = this;
	function start(){
		if(this_.div && this_.isClosedDiv.s1 && this_.isClosedDiv.s2){
			this_.div.style.display = "none";
			this_.isClosedDiv = this_.isClosedDivBackup;
		}
		setTimeout(start,this_.divStatusMonitoring);
	}
	start();
};
Unionmon_DRR.prototype.autoFindDoms = function (){
	var this_ = this;
	function start(){
		if(this_.isFindDoms || this_.currTimes >= this_.findTimes){
			return false; // 没有找到解析的元素
		}else{
			for(var domId in this_.Doms){
				if(!this_.$(domId)){
					this_.currTimes++;
					setTimeout(start,this_.findInterval);
					return false;
				}
				this_.DO[domId] = this_.$(domId);
			}
			this_.isFindDoms = true; // 成功的找到了所有的元素
		};
	}
	start();
};
Unionmon_DRR.prototype.bindEvents = function (){
	var this_ = this;
	//获取元素的Y标
	function getTop(e){
		var offset=e.offsetTop;
		if(e.offsetParent!=null){
			offset+=getTop(e.offsetParent);
		}
		return offset;
	}
	//获取元素的X标
	function getLeft(e){
		var offset=e.offsetLeft;
		if(e.offsetParent!=null){
			offset+=getLeft(e.offsetParent);
		}
		return offset;
	}
	function start(){
		if(this_.isFindDoms){
			var indexStr = this_.domIndexStr;
			var startPosition = this_.domStartPosition;
			var daLen = this_.DA.length;
			for(var key in this_.Doms){
				var dom = this_.DO[key];
				dom.onkeydown = function (){ // 只允许用户按切换键[Tab]
					if(event.keyCode != 9){
						return event.returnValue=false;
					}
				};
				dom.onfocus = function (){
					this_.isClosedDiv.s1 = false;
					this_.div.style.top = getTop(this) + this.offsetHeight;
					this_.div.style.left = getLeft(this);
					this_.div.style.display = "";

					var range = setRange(this_.contentDisplay,this);

					createDivHTML(range,this); // 生成div中的元素

					spanBind(range,this);

				};
				function createDivHTML(range,dom){ // 用户可以选择显示的内容
					var divHTML = "";
					for(var i=range.begin;i <= range.end;i++){
						var str = this_.spanStr.replace(this_.spanValue,i);
						str = str.replace(this_.spanIdSuf,i);
						divHTML += str;
					}
					this_.div.innerHTML = divHTML;

					if(this_.contentDisplay == "all"){
						var sc = this_.spanColor;
						var currRange = setRange("itself",this_.$(dom.id));
						for(var i=currRange.begin;i <= currRange.end;i++){
							var str = "this_.$(this_.spanName+i).style."+sc.styleName+"=sc[dom.id]";
							eval(str);
						}
					}
				}
				function spanBind(range,dom){ // 给span绑定事件
					var cdAll = function (){};

					if(this_.contentDisplay == "all"){
						function autoSubtraction(d1,symbol,d2,isAscending){ // 自动加减
							var boo = true;
							switch(symbol){
								case ">" :
									if(isAscending){
										boo = parseInt(d1.value) > parseInt(d2.value);
									}else{
										boo = parseInt(d1.value) < parseInt(d2.value);
									}
									if(!boo){
										if(isAscending){
											d2.value = parseInt(d1.value) - this_.digitalInterval;
										}else{
											d2.value = parseInt(d1.value) + this_.digitalInterval;
										}
									}
								break;
								case ">=" :
									if(isAscending){
										boo = parseInt(d1.value) >= parseInt(d2.value);
									}else{
										boo = parseInt(d1.value) <= parseInt(d2.value);
									}
									if(!boo){
										d2.value = parseInt(d1.value);
									}
								break;
								case "<=" :
									if(isAscending){
										boo = parseInt(d1.value) <= parseInt(d2.value);
									}else{
										boo = parseInt(d1.value) >= parseInt(d2.value);
									}
									if(!boo){
										d2.value = parseInt(d1.value);
									}
								break;
								case "<" :
									if(isAscending){
										boo = parseInt(d1.value) < parseInt(d2.value);
									}else{
										boo = parseInt(d1.value) > parseInt(d2.value);
									}
									if(!boo){
										if(isAscending){
											d2.value = parseInt(d1.value) + this_.digitalInterval;
										}else{
											d2.value = parseInt(d1.value) - this_.digitalInterval;
										}
									}
								break;
							}
							if(isAscending){
								return boo;
							}else{
								return !boo;
							}
						}
						cdAll = function (){
							var das = this_.DA;
							var dos = this_.DO;
							var domIndex = dos[dom.id+this_.domAtIndex];
							var index = dos[dom.id+this_.domIndexStr];
							if(domIndex > 1){
								var tem = index;
								while(tem > 0){
									var d1 = das[tem];
									if(isNaN(d1)){
										d1 = this_.$(d1);
									}
									var symbol = das[--tem];
									var d2 = das[--tem];
									if(isNaN(d2)){
										d2 = this_.$(d2);
									}else{
										break;
									}
									autoSubtraction(d1,symbol,d2,false);
								}
							}
							if(domIndex < this_.domsSize){
								var tem = index;
								while(tem < das.length){
									var d1 = das[tem];
									if(isNaN(d1)){
										d1 = this_.$(d1);
									}
									var symbol = das[++tem];
									var d2 = das[++tem];
									if(isNaN(d2)){
										d2 = this_.$(d2);
									}else{
										break;
									}
									autoSubtraction(d1,symbol,d2,true);
								}
							}
						};
					}
					for(var i=range.begin;i <= range.end;i++){
						if(dom.value == ""+i){
							var span = this_.$(this_.spanName+i);

							span.style.background = "#ccc";
							if(span.offsetTop > this_.div.offsetHeight){
								this_.div.scrollTop = span.offsetTop;
							}else{
								this_.div.scrollTop = 0;
							}
						}else{
							this_.$(this_.spanName+i).onmouseover = function (){
								this.style.background = "#ccc";
							};
							this_.$(this_.spanName+i).onmouseout = function (){
								this.style.background = "";
							};
						}
						this_.$(this_.spanName+i).onclick = function (){
							dom.value = this.innerHTML;
							this_.div.style.display = "none";
							this_.isClosedDiv = this_.isClosedDivBackup;
							cdAll(); // 如果值为all的话.这个方法就不是空的
						};
					};
				}
				function setRange(contentDisplay,dom){ // 设置范围
					var range = new Object();
					var begin = 0;
					var end = 0;

					var daIndex = this_.DO[dom.id+indexStr];
					if(contentDisplay == "itself"){
						if(daIndex >= startPosition){
							begin = this_.DA[daIndex-startPosition];
							if(isNaN(begin)){
								begin = this_.DO[begin].value;
							};
						}
						end = this_.DA[daLen-1];
						if(!this_.DA[dom.id+this_.domIsLastStr]){
							end = this_.DA[daIndex+startPosition];
							if(isNaN(end)){
								end = this_.DO[end].value;
							};
						}


						begin = parseInt(begin);
						if(this_.DA[daIndex-this_.digitalInterval] == ">"){
							begin = begin - this_.digitalInterval;
						}else if(this_.DA[daIndex-this_.digitalInterval] == "<"){
							begin = begin + this_.digitalInterval;
						}
						end = parseInt(end);
						if(this_.DA[daIndex+this_.digitalInterval] == ">"){
							end = end + this_.digitalInterval;
						}else if(this_.DA[daIndex+this_.digitalInterval] == "<"){
							end = end - this_.digitalInterval;
						}
					}else if(contentDisplay == "all"){
						begin = this_.DA[0];
						if(isNaN(begin)){
							begin = this_.DO[begin].value;
						};
						begin = parseInt(begin);

						end = this_.DA[this_.DA.length-1];
						if(isNaN(end)){
							end = this_.DO[end].value;
						};
						end = parseInt(end);

						if(daIndex != 0){
							var tem = daIndex;;
							while((tem-=this_.digitalInterval) >= 0){
								if(tem%2==1){
									if(this_.DA[tem] == ">"){
										begin = begin - this_.digitalInterval;
									}else if(this_.DA[tem] == "<"){
										begin = begin + this_.digitalInterval;
									}
								}
							}
						}
						if(daIndex != this_.DA.length-1){
							var tem = daIndex;;
							while((tem+=this_.digitalInterval) < this_.DA.length){
								if(tem%2==1){
									if(this_.DA[tem] == ">"){
										end = end + this_.digitalInterval;
									}else if(this_.DA[tem] == "<"){
										end = end - this_.digitalInterval;
									}
								}
							}
						}

					}
					range.begin = begin;
					range.end = end;
					return range;
				}
				dom.onblur = function (){
					this_.isClosedDiv.s1 = true;
				};
			};
		}else{
			if(this_.currTimes >= this_.findTimes){
				return false; // 元素没有找到.绑定方法失败
			}
			setTimeout(start,this_.findInterval);
		};
	}
	start();
};
/**
 * 分解字符串
 * @param str : 字符串
 */
Unionmon_DRR.prototype.decompose = function (str){
	var arr = [];
	var obj = new Object();
	var doms = new Object();
	if(!str){
		arr = this.expression.split(" ");
	}else{
		arr = str.split(" ");
	}

	var isCheckId = function (id){
		if(id.indexOf("<") > -1){
			return false;
		}
		if(id.indexOf("=") > -1){
			return false;
		}
		if(id.indexOf(">") > -1){
			return false;
		}

		return true;
	};
	for(var i=0; i<arr.length; i++){
		if(isNaN(arr[i]) && isCheckId(arr[i])){
			doms[arr[i]] = obj[arr[i]] = arr[i];
			obj[arr[i]+this.domIndexStr] = i;
			obj[arr[i]+this.domAtIndex] = ++this.domsSize;
			var boo = false;
			if((i+1) == arr.length){
				boo = true;
			}
			obj[arr[i]+this.domIsLastStr] = boo;
		};
	}

	this.DA = arr;
	this.DO = obj;
	this.Doms = doms;
};
